// 引入mongoose第三方模块
const mongoose = require('mongoose');
//引入string-random
const stringRandom = require("string-random");


// 创建用户集合规则
const PurchaseOrderSchema = new mongoose.Schema({
    //订单编号
    orderNo:{
        type: String,
        require: true,
        unique: true
    },
    //快递单号
    expressNo:{
        type:String
    },
    // 商品信息
    shopInfo:{
        type: Object
    },
    //订单创建时间
    createDate:{
        type: Date
    },
    //付款时间
    payDate:{
        type:Date
    },
    // 收货信息
    deliveryAddress:{
        type: Object
    },
    //订单状态
    state:{
        type: Object
    }
});

// 创建集合
const PurchaseOrder = mongoose.model('PurchaseOrder', PurchaseOrderSchema);
const path = require('path')

async function createorderNo(){
    var orderNo = stringRandom(16, { numbers: true });
    var orNo = await PurchaseOrder.findOne({orderNo:orderNo},{orderNo:1});
    while(orNo != null){
        orderNo = stringRandom(16, { numbers: true });
        artNo = await PurchaseOrder.findOne({orderNo:orderNo},{orderNo:1});
    }
    return orderNo;
}
//createorderNo();


async function createPurOrder (shopInfo,deliveryAddress) {
    //创建订单号
    var orderNo = stringRandom(16, { numbers: true });
    var orNo = await PurchaseOrder.findOne({orderNo:orderNo},{orderNo:1});
    while(orNo != null){
        orderNo = stringRandom(16, { numbers: true });
        artNo = await PurchaseOrder.findOne({orderNo:orderNo},{orderNo:1});
    }
	const purOrder = await PurchaseOrder.create({
        //订单编号
    orderNo: orderNo,
    //快递单号
    expressNo:"",
    // 商品信息
    shopInfo: shopInfo,
    //订单创建时间
    createDate:"",
    //付款时间
    payDate: "",
    // 收货信息
    deliveryAddress:deliveryAddress,
    OrderState:{stateCode: "0", stateMsg:"待付款"}
	});
    console.log("数据添加成功!");
}

//createPurOrder();


// 将用户集合做为模块成员进行导出
module.exports = {
   PurchaseOrder
}; 